package com.itheima.controller;

import com.itheima.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class ParamController {

// 1.请求对象接收参数
    @ResponseBody // 把返回的数据当做数据处理  不会当成页面处理  请求体
    @RequestMapping("/p1")  // 响应映射的名字
    public String p1(HttpServletRequest request) {
        System.out.println(request.getParameter("username"));
        return "{'info':Hello}";  // 这里返回的是键值对 检测是否会被当做数据处理
    }

// 2.变量接收
    @ResponseBody
    @RequestMapping("/p2")
    public String p2(String username, int age) {
        System.out.println("username = " + username);
        System.out.println("age = " + age);
        return "{'info':Hello}";
    }

// 3.注解起别名
    @ResponseBody
    @RequestMapping("/p3")
    /**
     * value===》》》别名
     * required===》》》是否为必须输入
     * defaultValue===》》》默认值
     */
    public String p3(@RequestParam(value = "name",required = false,defaultValue = "佚名") String username, int age) {
        System.out.println("username = " + username);
        System.out.println("age = " + age);
        return "{'info':Hello}";
    }

// 4.pojo类
    @ResponseBody
    @RequestMapping("/p4")
    public String p4(User user) {
        System.out.println(user);
        return "{'info':Hello}";
    }
    // 4.1 pojo类里嵌套pojo类
    @ResponseBody
    @RequestMapping("/p41")
    public String p41(User user) {
        System.out.println(user);
        return "{'info':Hello}";
    }

// 5.集合类型
    @ResponseBody
    @RequestMapping("/p5")
    public String p5(@RequestParam List<String> list) {
        System.out.println(list);
        return "{'info':Hello}";
    }
    // map集合
    @ResponseBody
    @RequestMapping("/p6")
    public String p6(@RequestParam Map map) {
        System.out.println(map);
        return "{'info':Hello}";
    }

    // 数组
    @ResponseBody
    @RequestMapping("/p7")
    public String p7(String[] strings) {
        System.out.println(Arrays.toString(strings));
        return "{'info':Hello}";
    }



    // 日期
    @ResponseBody
    @RequestMapping("/p8")
    public String p8(Date date,
                     @DateTimeFormat(pattern = "yyyy-MM-dd") Date date1,
                     @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date date2
                     ) {
        System.out.println(date);
        System.out.println(date1);
        System.out.println(date2);

        return "{'info':Hello}";
    }


















}
